package edu.csl.study.coreserver2.controller;

import edu.csl.study.coreserver2.feign.MyFeignClient;
import edu.csl.study.coreserver2.hystrix.ServiceWithHystrix;
import edu.csl.study.coreserver2.service.HelloService;
import edu.csl.study.coreserver2.service.HelloServiceImpl;
import edu.csl.study.coreserver2.vo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Description: 将MyFeignClient注入到controller层
 */
@RestController
@RefreshScope
public class ConsumerController {
    Logger logger = LoggerFactory.getLogger(ConsumerController.class);
    @Autowired
    HelloService helloService;
    @Autowired
    MyFeignClient myFeignClient;
    @Autowired
    ServiceWithHystrix serviceWithHystrix;
    @Value("${env.name:defalut0}")
    public String env;
    @Value("${env.name1:defalut1}")
    public String env1;
    @Value("${env.name2:defalut2}")
    public String env2;

    @RequestMapping("/hello")
    public String index(HttpServletRequest request) throws InterruptedException {
        //Thread.sleep(2000);
        String params = request.getHeader("X-Request-Foo");
        logger.info("server2 - HelloController - hello 方法");
        return helloService.hello() +"  env.name="+env+" env.name1="+env1+" env.name2="+env2+"  core2中参数-X-Request-Foo = " + params;
    }
    @RequestMapping("/mypath/hello")
    public String index2(HttpServletRequest request) {
        String params = request.getHeader("X-Request-Foo");
        logger.info("server2 - HelloController - /mypath/hello/方法");
        return "server2 - HelloController - /mypath/hello/方法 - "+"  参数-X-Request-Foo = " + params ;
    }

    @RequestMapping("/user/post")
    public String postUser() {
        User user = new User();
        user.setUserName("user2");
        user.setPassword("user2");

        return  myFeignClient.updateUser2(user) ;
    }
    @RequestMapping("/user/get3")
    public String postUser3() {
        return  myFeignClient.updateUser3("user3","user3") ;
    }

    @RequestMapping("/hystrix/{id}")
    public String hystrix(@PathVariable(name = "id") int id) throws InterruptedException {

        return serviceWithHystrix.getUser(id) ;
    }
}